﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using S4Store.Service;
using S4Store.Domain;
using S4Store.Models.Information;
using LX.Core.Utils;

namespace S4Store.Controllers
{
    public class InformationController : ReportBaseController
    {
        public InformationService InformationService { get; set; }

       

        public ActionResult Index()
        {
                return View();            
        }


        public ActionResult Query()
        {
            QueryModel query = new QueryModel
            {
                Page = 1,
                PageSize = 20
            };
            if (CurrentUser.Position == Position.SalesManager)
            {
                query.Type = Information.InformationType.Sale.ToInt();
            }
            else if (CurrentUser.Position == Position.MarketingManager)
            {
                query.Type = ImproveGuide.ImproveGuideType.Markting.ToInt();
            }
            else if (CurrentUser.Position == Position.AftersalesManager)
            {
                query.Type = ImproveGuide.ImproveGuideType.AfterSale.ToInt();
            }
            return Query(query);
        }

        [HttpPost]
        public ActionResult Query(QueryModel query)
        {
            using (var t = new Transtion())
            {
                t.Inject(InformationService);

                var querysource = InformationService.GetQuery();
                if (query.Type != 0)
                {
                    querysource = querysource.Where(x => x.TypeValue == query.Type);
                }
                if (!String.IsNullOrWhiteSpace(query.Title))
                {
                    querysource = querysource.Where(x => x.Title.Contains(query.Title));
                }
                int total = querysource.Count();
                querysource = querysource.OrderByDescending(x => x.Id);
                if (query.Page > 1 && query.PageSize > 0)
                {
                    querysource = querysource.Skip((query.Page - 1) * query.PageSize);
                }
                querysource = querysource.Take(query.PageSize);
                LX.Core.Query.PagedList<Information> data = new LX.Core.Query.PagedList<Information>
                {
                    Data = querysource.ToList(),
                    Page = query.Page,
                    PageSize = query.PageSize,
                    Total = total
                };
                return View(data);
            }
        }

        public ActionResult Detail(int id){
            using (var t = new Transtion())
            {
                t.Inject(InformationService);

                var querysource = InformationService.GetQuery();
                var model = querysource.FirstOrDefault(x => x.Id == id);
                t.Commit();
                return View(model);
            }
        }


        public ActionResult Add()
        {
            return View();
        }

        [HttpPost]
        [ValidateInput(false)]
        public JsonResult Add(Information model)
        {
            using (var t = new Transtion())
            {
                t.Inject(InformationService);
                InformationService.AddInformation(model);
                t.Commit();
                return new JsonResult
                {
                    Data = new
                    {
                        success = true
                    }
                };
            }
        }

        public ActionResult Modify(int id)
        {
            using (var t = new Transtion())
            {
                t.Inject(InformationService);

                var querysource = InformationService.GetQuery();
                var model = querysource.FirstOrDefault(x => x.Id == id);
                t.Commit();
                return View(model);
            }
        }

        [HttpPost]
        [ValidateInput(false)]
        public JsonResult ModifyBack(Information model)
        {
            using (var t = new Transtion())
            {
                t.Inject(InformationService);
                var querysource = InformationService.GetQuery();
                var rmodel = querysource.FirstOrDefault(x => x.Id == model.Id);
                rmodel.Title = model.Title;
                rmodel.Type = model.Type;
                rmodel.Content = model.Content;
                InformationService.ModifyInformation(rmodel);
                t.Commit();
                return new JsonResult
                {
                    Data = new
                    {
                        success = true
                    }
                };
            }

        }

        [HttpPost]
        public JsonResult Delete(List<int> ids)
        {
            CheckPower(Position.SystemManager);
            if (Helper.ArrayIsEmpty(ids))
            {
                throw new Exception("请选择要删除的项");
            }
            using (var t = new Transtion())
            {
                t.Inject(InformationService);
                foreach (var id in ids)
                {
                    InformationService.DeleteInformation(id);
                }
                t.Commit();
            }
            return new JsonResult
            {
                Data = new
                {
                    success = true,
                    result = "删除成功"
                },
                JsonRequestBehavior = JsonRequestBehavior.AllowGet
            };
        }

    }
}
